package xyz.xpman.prisonerfitness.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.kit.DonateKit;
import xyz.xpman.prisonerfitness.kit.QQKitProxy;

public class AboutActivity extends BaseActivity {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView() {
        // 文字高亮使用的颜色
        @SuppressLint("ResourceType")
        final int highlightColor =
                Color.parseColor(getResources().getString(R.color.orange));


        addIndentation(findViewById(R.id.description));


        // 关于
        TextView about = findViewById(R.id.about);
        addIndentation(about);
        // 对“捐助”二字做高亮显示
        highlightKeywords(about, "《囚徒健身》", highlightColor);
        // 对“捐助”二字做高亮显示
        highlightKeywords(about, "六艺十式", highlightColor);

        // 提示
        TextView tips = findViewById(R.id.tips);
        addIndentation(tips);
        // 点击捐助事件
        tips.setOnClickListener(view -> DonateKit.run(this));

        // 对“捐助”二字做高亮显示
        highlightKeywords(tips, "捐助", highlightColor);


        // 加入qq群view
        View joinQQGroup = findViewById(R.id.join_qq_group);
        joinQQGroup.setOnClickListener(view -> QQKitProxy.toJoinGroupPage(this));
    }

    /**
     * 对“捐助”二字做高亮显示
     */
    @SuppressLint({"ResourceAsColor", "ResourceType"})
    private void highlightKeywords(@NotNull TextView view, @NotNull String keywords, int color) {
        SpannableString s = new SpannableString(view.getText());

        Pattern p = Pattern.compile(keywords);
        Matcher m = p.matcher(s);

        while (m.find()) {
            int start = m.start();
            int end = m.end();
            s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        view.setText(s);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }

}